# coding: utf-8
# from Crypto.Cipher import AES
import hashlib
from django.contrib.auth.models import AbstractBaseUser, User, PermissionsMixin, BaseUserManager
from django.db import models
from django.utils import timezone
from django.conf import settings
from common.models.base import BaseModel
from common.models.position import JPositionCounty, JPositionTown, JPositionVillage


SEX_TYPE = (
    ('M', u'男'),
    ('F', u'女'),
    ('S', u'密秘')
)


def hashed_password(raw_password):
    return hashlib.md5(raw_password).hexdigest()


class UserManager(BaseUserManager):
    def create_user(self, username, email=None, raw_password=None, **extra_fields):
        now = timezone.now()
        if not username:
            raise ValueError('The given username must be set')

        email = UserManager.normalize_email(email)
        user = self.model(username=username, email=email,
                          is_staff=False, is_active=True, is_superuser=False,
                          last_login=now, raw_password=raw_password, **extra_fields)

        user.password = hashed_password(raw_password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email=None, password=None, **extra_fields):
        u = self.create_user(username, raw_password=password, email=email, **extra_fields)
        u.is_staff = True
        u.is_active = True
        u.is_superuser = True
        u.save(using=self._db)
        return u


class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=128, verbose_name=u'用户名', unique=True)
    raw_password = models.CharField(max_length=128, verbose_name=u'原始密码')
    email = models.EmailField(unique = True, verbose_name='Email', null=True)

    class Meta:
        app_label = 'common'
        db_table = 'user_user'

    USERNAME_FIELD = 'username'    # 作为用户登录认证用的字段，可以usename，或者email等，但必须是唯一的。
    REQUIRED_FIELDS = []  # 使用createsuperuser命令创建超级用户时提示操作者输入的字段

    is_staff = models.BooleanField('staff status', default=False,
        help_text='Designates whether the user can log into this admin site.')

    is_active = models.BooleanField('active', default=True,
        help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.')

    @property
    def avatar_url(self):
        return self.userprofile.avatar.url if self.userprofile.avatar else ''

    def get_full_name(self):
        return self.username.strip()

    def get_short_name(self):
        return self.username.strip()

    def check_password(self, raw_password):
        print hashed_password(raw_password)
        print self.password
        if hashed_password(raw_password) == self.password:
            return True
        return False

    # def set_raw_password(self, raw_password):
    #     d = AES.new(settings.AES_PWD)
    #     x_content = raw_password + (16 - len(raw_password)%16)*'u'    # 被加密字符串要是16的倍数，所以不够的用u填充
    #     encrypted_content = d.encrypt(x_content)
    #     self.raw_password = encrypted_content

    objects = UserManager()


class UserProfile(BaseModel):
    user = models.OneToOneField(User, verbose_name=u'用户')

    true_name = models.CharField(max_length=128, verbose_name=u'真实名', null=True)
    sex = models.CharField(max_length=1, choices=SEX_TYPE, verbose_name=u'性别', default='S')
    declaration = models.CharField(max_length=200, verbose_name=u'个性签名', default='')
    avatar = models.ImageField(upload_to='static/var/', max_length=255, verbose_name=u'头像', default='static/var/default.png')

    county = models.ForeignKey(JPositionCounty, null=True)
    town = models.ForeignKey(JPositionTown, null=True)
    village = models.ForeignKey(JPositionVillage, null=True)

    class Meta:
        app_label = 'common'
        db_table = 'user_profile'

    @property
    def user_position(self):
        s = ''
        s += self.county.county_name if self.county else ''
        s += self.town.town_name if self.town else ''
        s += self.village.village_name if self.village else ''
        return s

    @property
    def user_position_simple(self):
        s = ''
        s += self.county.county_name if self.county else ''
        s += self.town.town_name if self.town else ''
        return s